﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

using System;
using Glodon.Gdmp.DB;
using Glodon.Gdmp.UI;

namespace Glodon.Gdmp.SDK.Samples.LineTypeChange
{
    /// <summary>
    /// 此示例展示了模型线使用新创建的线样式以替换默认的实线样式。
    /// </summary>
    public class LineTypeChangeCmd : IExternalCommand
    {
        public Result Execute(UIDocument uiDocument)
        {
            try
            {
                if (null == uiDocument.Document)
                {
                    return Result.Failed;
                }
                Document document = uiDocument.Document;

                // 创建线型。
                LineTypeData lineTypeData = new LineTypeData("BORDER");
                lineTypeData.Name = "*BORDER";
                lineTypeData.Description = "Border __ __ . __ __ . __ __ . __ __ . __ __ .";
                lineTypeData.Pattern = "A,.5,-.25,.5,-.25,0,-.25";

                using (var transaction = new Transaction(document, "创建线型"))
                {
                    transaction.Start();

                    LineType lineType = LineType.Create(document, lineTypeData, true);
                    // 设置线型。
                    GraphicsStyle graphicsStyle = GraphicsStyle.Create(document);

                    // 创建图形样式
                    GraphicsStyleData graphicsStyleData = GraphicsStyleData.Create();
                    graphicsStyleData.ProjectionLineTypeName = lineType.Data.Name;
                    graphicsStyleData.SectionLineTypeName = lineType.Data.Name;
                    graphicsStyle.SetGraphicsStyleData(graphicsStyleData);

                    // 创建模型线并修改其样式。
                    Line3d line3d = Line3d.CreateBound(new Vector3d(1, 0, 0), new Vector3d(1000, 0, 0));
                    ModelCurve modelCurve = ModelCurve.Create(document, line3d);
                    modelCurve.SetGraphicsStyleId(graphicsStyle.Id);

                    transaction.Commit();
                }
                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                var message = ex.Message;
                return Result.Failed;
            }
        }
    }
}
